package com.xnfh.framework.utils

import android.app.Activity
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.xnfh.framework.R
import com.xnfh.framework.log.LogUtil
import com.xnfh.framework.toast.TipsToast

/**
 * @Description:
 * @Author: ziheng
 * @CreateDate: 2023/11/10 16:55
 * @UpdateUser: ziheng
 * @UpdateDate: 2023/11/10 16:55
 * @UpdateRemark: 更新说明
 */
object PermissionUtils {

    interface PermissionImp{
        fun onSuccess(allGranted: Boolean)
        fun onField(){}
    }

    fun openGPS(context: Activity,permissionImp: PermissionImp) {
        LogUtil.d("进入了openGPS（）方法")
        XXPermissions.with(context)
            .permission(Permission.ACCESS_COARSE_LOCATION, Permission.ACCESS_FINE_LOCATION)
            .request(object : OnPermissionCallback {
                override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                    LogUtil.d("onGranted（）方法")
                    permissionImp.onSuccess(allGranted)
                    if (!allGranted) {
                        TipsToast.showTips(context.resources.getString(R.string.permission_tips))
                        return
                    }
                }

                override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                    LogUtil.d("onDenied（）方法")
                    if (doNotAskAgain) {
                        TipsToast.showTips(context.resources.getString(R.string.permission_start_tips))
                        // 如果是被永久拒绝就跳转到应用权限系统设置页面
                        XXPermissions.startPermissionActivity(context, permissions)
                    } else {
                        TipsToast.showTips(context.resources.getString(R.string.permission_error))
                    }
                    permissionImp.onField()
                }

            })
    }


    /**
     * 开启读写权限
     */
    fun requestPermissions(context: Activity){
        val list = ArrayList<String>()
//        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU){
            list.add(Permission.READ_MEDIA_AUDIO)
            list.add(Permission.READ_MEDIA_IMAGES)
            list.add(Permission.READ_MEDIA_VIDEO)
//        }else{
//            list.add(Permission.WRITE_EXTERNAL_STORAGE)
//        }
        XXPermissions.with(context)
            .permission(list)
            .request(object :OnPermissionCallback{
                override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                    if (!allGranted) {
                        TipsToast.showTips(context.resources.getString(R.string.permission_tips))
                        return
                    }
                }
            })

    }

    /**
     * 开启拨打电话权限
     */
    fun requestCallPhonePermission(context: Activity,permissionImp: PermissionImp){
        XXPermissions.with(context).permission(Permission.CALL_PHONE).request(object :OnPermissionCallback{
            override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                permissionImp.onSuccess(true)
            }

            override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                super.onDenied(permissions, doNotAskAgain)
                TipsToast.showTips(context.resources.getString(R.string.permission_error))
            }
        })
    }

}